/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.progress.WorkbenchJob;
import es.upm.dit.gsi.eclipse.jadex.navigator.Child;

/**
 * Action class for the launch wizard option of the subchilds menu
 * 
 * @author Pablo Muñoz
 */
public class LaunchWizardAction implements IActionDelegate{
	ISelection _selection;

	public LaunchWizardAction()  {
		super();
	}
	
	@Override
	public void run(IAction action) {
		final IFile agentFile = getSelectedAgentFile();
		final IActionDelegate wizard = getActionToRun();
		WorkbenchJob job = new WorkbenchJob(Display.getCurrent(),"Launch Wizard") {
			
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				final Action action = new Action() {};
				wizard.selectionChanged(action, new StructuredSelection(agentFile));
	  			wizard.run(action);
			return Status.OK_STATUS;
			}
		};
		job.schedule();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		_selection = selection;
	}
	
	/*
	 * Gets the action for running a "new" assistant depending on the selected subchild
	 */
	private IActionDelegate getActionToRun(){
		String childType = getSelectedChild().getName();
		if(childType.equals("beliefs")){
			return new NewBeliefAction();
		}
		else if(childType.equals("capabilities")){
			return new NewCapability();
		}
		else if(childType.equals("configurations")){
			return new NewConfigurationAction();
		}
		else if(childType.equals("events")){
			return new NewEventAction();
		}
		else if(childType.equals("expressions")){
			return new NewExpressionAction();
		}
		else if(childType.equals("goals")){
			return new NewGoalAction();
		}
		else if(childType.equals("imports")){
			return new NewImportAction();
		}
		else if(childType.equals("plans")){
			return new NewPlanAction();
		}
		else if(childType.equals("properties")){
			return new NewPropertyAction();
		}
		return null;						
	}
	
	
	/*
	 * Gets the agent file corresponding to the selected child
	 */
	private IFile getSelectedAgentFile(){
		Child selChild = getSelectedChild();
		if(selChild != null){
			return selChild.getContainer();
		}
		return null;
	}
	
	
	/*
	 * Gets the selected child
	 */
	private Child getSelectedChild(){
		if(_selection instanceof TreeSelection){
			Object selectedObject = ((TreeSelection) _selection).getFirstElement();
			if(selectedObject instanceof Child){
				return (Child) selectedObject;
			}
		}
		return null;
	}
}
